﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:tv="clr-namespace:TableView">

  <Brush x:Key="Brush_Header">CadetBlue</Brush>
  <Brush x:Key="Brush_Selected">LightBlue</Brush>
  <Brush x:Key="Brush_Focus">Orange</Brush>
  <Brush x:Key="Brush_SortIndicator">Orange</Brush>

  <!--Column Header Style-->
  <Style x:Key="columnHeaderStyle"
         TargetType="{x:Type tv:TableViewColumnHeader}">
    <Setter Property="Focusable"
            Value="False" />
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
    <Setter Property="Background"
            Value="{StaticResource Brush_Header}" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type tv:TableViewColumnHeader}">
          <Grid x:Name="header">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="AUTO" />
              <ColumnDefinition Width="AUTO" />
              <ColumnDefinition Width="AUTO" />
            </Grid.ColumnDefinitions>
            <ContentPresenter Grid.Column="0"
                              Content="{Binding Title}"
                              ContentTemplate="{TemplateBinding ContentTemplate}" />
            <TextBlock Grid.Column="1"
                       x:Name="sortOrder"
                       Text="{Binding SortOrder}" />
            <Polygon Grid.Column="2"
                     x:Name="sortUp"
                     Points="4,0 0,8 8,8"
                     Fill="{DynamicResource Brush_SortIndicator}"
                     Stroke="Transparent"
                     Stretch="Uniform"
                     Visibility="Collapsed"
                     StrokeThickness="1" />
            <Polygon Grid.Column="2"
                     x:Name="sortDown"
                     Points="0,0 8,0 4,8"
                     Fill="{DynamicResource Brush_SortIndicator}"
                     Stroke="Transparent"
                     Stretch="Uniform"
                     Visibility="Collapsed"
                     StrokeThickness="1" />

            <tv:TableViewHeaderThumb Grid.Column="3"
                                     Width="2"
                                     HorizontalAlignment="Right"
                                     Cursor="SizeWE">
            </tv:TableViewHeaderThumb>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
              <Setter TargetName="header"
                      Property="Background"
                      Value="{DynamicResource ResourceKey={x:Static SystemColors.HotTrackBrushKey}}" />
            </Trigger>

            <DataTrigger Binding="{Binding SortDirection}"
                         Value="{x:Static tv:TableViewColumn+ColumnSortDirection.Up}">
              <Setter TargetName="sortUp"
                      Property="Visibility"
                      Value="Visible" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SortDirection}"
                         Value="{x:Static tv:TableViewColumn+ColumnSortDirection.Down}">
              <Setter TargetName="sortDown"
                      Property="Visibility"
                      Value="Visible" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SortOrder}"
                         Value="0">
              <Setter TargetName="sortOrder"
                      Property="Visibility"
                      Value="Collapsed" />
            </DataTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--Cell Container Style-->
  <Style x:Key="cellContainerStyle"
         TargetType="tv:TableViewCell">
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="tv:TableViewCell">
          <Border x:Name="selector"
                  Background="#00000000"
                  Padding="1">
            <ContentPresenter Content="{Binding}"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              ContentTemplate="{TemplateBinding ContentTemplate}" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
              <Setter TargetName="selector"
                      Property="Background"
                      Value="{StaticResource Brush_Selected}" />
            </Trigger>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter TargetName="selector"
                      Property="Background"
                      Value="{StaticResource Brush_Focus}" />
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="True">
              <Setter TargetName="selector"
                      Property="Background"
                      Value="{DynamicResource ResourceKey={x:Static SystemColors.HotTrackBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--Cell Presenter Style-->
  <Style x:Key="cellsPresenterStyle"
         TargetType="tv:TableViewCellsPresenter">
    <Setter Property="ItemContainerStyle"
            Value="{StaticResource cellContainerStyle}" />
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <tv:TableViewCellsPanel />
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="tv:TableViewCellsPresenter">
          <Border x:Name="focusBdr">
            <ItemsPresenter />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
              <Setter TargetName="focusBdr"
                      Property="Background"
                      Value="{StaticResource Brush_Selected}" />
            </Trigger>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter TargetName="focusBdr"
                      Property="Background"
                      Value="{StaticResource Brush_Focus}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--Header Presenter Style-->
  <Style x:Key="headerPresenterStyle"
         TargetType="{x:Type tv:TableViewHeaderPresenter}">
    <Setter Property="Focusable"
            Value="False" />
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <tv:TableViewHeaderPanel x:Name="PART_HeaderPanel"
                                   SnapsToDevicePixels="True" />
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="tv:TableViewHeaderPresenter">
          <ScrollViewer CanContentScroll="True"
                        x:Name="sv"
                        Focusable="False"
                        Padding="0"
                        HorizontalScrollBarVisibility="Hidden"
                        VerticalScrollBarVisibility="Hidden">
            <ItemsPresenter />
          </ScrollViewer>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--Rows Presenter Style-->
  <Style x:Key="rowsPresenterStyle"
         TargetType="{x:Type tv:TableViewRowsPresenter}">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <tv:TableViewRowsPanel x:Name="PART_RowsPanel"
                                 SnapsToDevicePixels="True" />
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="VirtualizingStackPanel.IsVirtualizing"
            Value="True" />
    <Setter Property="VirtualizingStackPanel.VirtualizationMode"
            Value="Recycling" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="tv:TableViewRowsPresenter">
          <ScrollViewer Name="PART_ScrollViewer"
                        CanContentScroll="True"
                        Focusable="False"
                        VerticalScrollBarVisibility="Auto"
                        HorizontalScrollBarVisibility="Auto"
                        VerticalContentAlignment="Center">
            <ItemsPresenter />
          </ScrollViewer>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!--TableView Style-->
  <Style TargetType="{x:Type tv:TableView}">
    <Setter Property="AllowDrop"
            Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="tv:TableView">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="AUTO" />
              <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <tv:TableViewHeaderPresenter Grid.Row="0"
                                         x:Name="PART_HeaderPresenter"
                                         ItemContainerStyle="{StaticResource columnHeaderStyle}"
                                         ItemsSource="{Binding Path=Columns, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type tv:TableView}}}"
                                         Style="{StaticResource headerPresenterStyle}" />

            <tv:TableViewRowsPresenter Grid.Row="1"
                                       x:Name="PART_RowsPresenter"
                                       ItemContainerStyle="{StaticResource cellsPresenterStyle}"
                                       ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type tv:TableView}}}"
                                       Style="{StaticResource rowsPresenterStyle}" />
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>
